home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / VANTAGE_ / VFIL_SKE.C < prev   
C/C++ Source or Header  |  1989-09-15  |  8KB  |  297 lines

  1. /**************************************************************/
  2.  
  3. /*    This is a sample Vantage VFIL written in LightSpeed C V4.0.
  4.     It pretends to support Microsoft Word V4.0 files, but it's
  5.     only a skeleton - no conversion is actually performed. (Sorry,
  6.     but I'm not permitted to disclose the format of a Word file.)
  7.     It's purpose is only to show the structure of a Vantage VFIL.
  8.     Feel free to use it, abuse it, or distribute it as you see fit.
  9.  
  10.     To build the VCMD resource, set the following project types:
  11.  
  12.         Code Resource
  13.         File Type        =    VEXT
  14.         Creator            =    VEXI
  15.         Custom Header    =    ON (checked)
  16.         Type            =    VFIL
  17.         ID                =    1 (arbitrary)
  18.         Attrs            =    00
  19.         
  20.     Also include the MacTraps library and check the
  21.     MacHeaders option in the code generation options dialog.
  22.     
  23.     
  24.                             Dave McWherter, 9/15/89
  25.                             Signature Software
  26.                             215-639-8764
  27. */
  28.     
  29.  
  30. /**************************************************************/
  31.  
  32. #include    <MacTypes.h>
  33.  
  34. /**************************************************************/
  35. /* Data type aliases:    */
  36.  
  37. #ifndef    bool
  38.     #define    bool    int
  39. #endif
  40.  
  41. #ifndef uchar
  42.     #define    uchar    unsigned char
  43. #endif
  44.  
  45. /****************************************************************/
  46. /* Status codes for Vantage:    */
  47.  
  48. #define    OK    0
  49. #define    ERR    -1
  50.  
  51. /****************************************************************/
  52. /* Prototypes for our routines:    */
  53.  
  54.         void    header(void);
  55.         
  56.         void    main(void);
  57.         
  58. pascal    int open_close(char **errmsg);
  59.  
  60. pascal    int        convert(Handle olddata, long oldlength, long creator, char *filename,
  61.                             int vrefnum, Handle *newdata, long *newlength, char **errmsg);
  62.     
  63.         uchar    getbyte(void);
  64.         
  65.         unsigned int    getword(void);
  66.         
  67.         unsigned long    getlong(void);
  68.                         
  69.         void    return_errmsg(char *msg, char **msgptr);
  70.         
  71. /****************************************************************/
  72. /* Our custom code header used by Vantage to call the VCMD.
  73.  
  74.     Several things to note here:
  75.  
  76.     1.) Be very careful of the alignment forced by the dc.b statement.
  77.         LSC aligns each dc.b. I don't know of any other way in LSC to
  78.         generate these Pascal strings needed in the Vantage header, which
  79.         means every time you change the strings, you have to hand-count
  80.         the bytes, change the size bytes, adjust the pad bytes, and worry
  81.         about alignment.
  82.     
  83.     2.) LSC can only generate PC-relative offsets in the dc.w statement.
  84.         Since Vantage wants header-relative offsets for each of the entry
  85.         points, we have to correct the offsets generated by the LSC assembler
  86.         in the header by +2 for Close, and +4 for Convert.
  87.     
  88.     3.) Since we're using a custom header, we have to load A0 ourselves
  89.         on entry to each routine. (Actually, we only need A0 setup so that
  90.         RememberA0() works, and we only need RememberA0 if we need globals.
  91.         Only our Execute routine needs globals (implicitly for the error
  92.         strings), so we really only need the A0 setup and SetUpA4() and
  93.         RestoreA4() stuff in the Execute routine. But I put it all in
  94.         every routine just for consistency's sake.)
  95. */
  96.  
  97. void header()
  98. {
  99.  
  100.     asm    {
  101.     
  102.             dc.w    @1+0    ; Offset to Open routine
  103.             dc.w    @2+2    ; Offset to Close routine
  104.             dc.w    @3+4    ; Offset to Convert routine
  105.             dc.l    'WDBN'    ; File type we handle
  106.                             
  107.         ; The prompt displayed during file conversion by Vantage...
  108.             dc.b    15, 'W', 'o', 'r', 'd', ' ', '4', '.', '0', ' ', 'V', 'F', 'I', 'L', '.', ' '
  109.             
  110.         ; The VFIL's name in the installer...
  111.             dc.b    13, 'W', 'o', 'r', 'd', ' ', '4', '.', '0', ' ', 'V', 'F', 'I', 'L'
  112.  
  113. ; *** Special note: Vantage V1.0 and 1.1 have a problem displaying the text of the
  114. ; *** prompt. Only an empty dialog is displayed. Vantage V2.0 fixes this, but you
  115. ; *** might not want to use a prompt because of this. Or, you could enable or disable
  116. ; *** the prompt by changing your header dynamically depending on the Vantage version.
  117.  
  118.         @1:    lea        header,a0        ; We must load A0 ourselves so RememberA0() works
  119.             jmp        open_close
  120.         
  121.         @2:    lea        header,a0
  122.             jmp        open_close
  123.         
  124.         @3:    lea        header,a0
  125.             jmp        convert
  126.         
  127.     }
  128.  
  129. }
  130.  
  131. /**************************************************************/
  132. /* Include LSC's magical A0 and A4 stuff...    */
  133. /* This must be somewhere _after_ the header() function.    */
  134.  
  135. static    __GetA4(void);
  136. void    SetUpA4(void);
  137.  
  138. #include    <SetupA4.h>
  139.  
  140. /****************************************************************/
  141.  
  142. void main()
  143. {
  144.  
  145.     /* We don't use a main()    */
  146. }
  147.  
  148. /****************************************************************/
  149. /* Called once when Vantage is opened. Use it to do any necessary
  150.     setup. If you return an error code, then your VFIL resource is
  151.     dumped and won't be called again.
  152.     
  153.     *** Since we don't need any special open or close processing,
  154.         our open and close routines can be the same.    */
  155.  
  156. pascal int open_close(errmsg)
  157.     char    **errmsg;
  158. {
  159.  
  160. /* We don't need any open or close processing
  161.  
  162.     RememberA0();
  163.     SetUpA4();
  164.  
  165.     do_open_or_close_stuff();
  166.     
  167.     RestoreA4();
  168.     
  169. */
  170.  
  171.     return OK;
  172.  
  173. }
  174.  
  175. /****************************************************************/
  176. /* Globals:    */
  177.  
  178. char    *rdata;        /* raw file data read by Vantage and passed to us    */
  179. long    roff;        /* current offset into above                        */
  180.  
  181. bool    error;        /* TRUE if any error occurred    */
  182.  
  183. /****************************************************************/
  184. /* This is our Convert routine that extracts the text from the file
  185.     and returns it to Vantage.    */
  186.  
  187. pascal int convert(olddata, oldlength, creator, filename, vrefnum, newdata, newlength, errmsg)
  188.     Handle    olddata;
  189.     long    oldlength;
  190.     long    creator;
  191.     char    *filename;
  192.     int        vrefnum;
  193.     Handle    *newdata;
  194.     long    *newlength;
  195.     char    **errmsg;
  196.  
  197. {
  198.  
  199.     long    textsize;
  200.     Handle    texthdl;
  201.     uchar    *text, c;
  202.     long    *ticks;
  203.     
  204.     RememberA0();
  205.     SetUpA4();
  206.     
  207.     *newdata = *newlength = 0;
  208.     texthdl = 0;
  209.     error = FALSE;
  210.     rdata = *olddata;        /* make raw data ptr global so subroutines can get at it    */
  211.     
  212.  
  213.     /* Do your conversion here. Get yourself a handle for the converted
  214.         text into 'texthdl' and stuff the converted text into it. Use
  215.         getbyte(), getword(), and getlong() to read data from the raw file
  216.         data, and control the read position by setting 'roff' to where you
  217.         want to read from. Put the size of the converted text into 'textsize'.    */
  218.  
  219.     /* Special note: If your VFIL supports a file type that Vantage already supports
  220.         internally, then your VFIL replaces Vantage's internal code for handling all
  221.         files of that type. This means that your VFIL should not only support newer
  222.         file formats for that type than Vantage does, but it also must support the old
  223.         formats too. For example, the real Word 4.0 VFIL also provides support for
  224.         Word 3.0 files, because Vantage gives all files of type WDBN to the VFIL,
  225.         whether Vantage could internally support them or not.    */
  226.  
  227.     
  228.     if ((texthdl && !error)) {
  229.         HUnlock(texthdl);
  230.         *newdata = texthdl;        /* Pass the converted text back to Vantage    */
  231.         *newlength = textsize;
  232.         
  233. /*        Delay(60, &ticks);    */    /* Prompt delay    so they can see it (if you use a prompt)    */
  234.         
  235.         RestoreA4();
  236.         return OK;
  237.     }
  238.     else {
  239.         if (texthdl)
  240.             DisposHandle(texthdl);
  241.         RestoreA4();
  242.         return ERR;
  243.     }
  244. }
  245.                         
  246. /****************************************************************/
  247. /* Get a byte from raw file data starting at roff and bump roff by 1. */
  248.  
  249. uchar getbyte()
  250. {
  251.  
  252.     return rdata[roff++];
  253.     
  254. }
  255.  
  256. /****************************************************************/
  257. /* Get a word from raw file data starting at roff and bump roff by 2. */
  258.  
  259. unsigned int getword()
  260. {
  261.     unsigned int    res;
  262.     
  263.     res = getbyte() << 8;
  264.     res |= getbyte();
  265.     
  266.     return res;
  267. }
  268.  
  269. /****************************************************************/
  270. /* Get a long from raw file data starting at roff and bump roff by 4. */
  271.  
  272. unsigned long getlong()
  273. {
  274.  
  275.     unsigned long    res;
  276.     
  277.     res = getword() << 16;
  278.     res |= getword();
  279.     
  280.     return res;
  281. }
  282.  
  283. /****************************************************************/
  284. /* Returns a pointer to an error message to Vantage.    */
  285.  
  286. void return_errmsg(msg, msgptr)
  287.     char    *msg;
  288.     char    **msgptr;
  289. {
  290.  
  291.     *msgptr = msg;
  292.     error = TRUE;
  293. }
  294.  
  295. /****************************************************************/
  296.  
  297.